package com.my.commonlibrary.activity;

import android.database.sqlite.SQLiteDatabase;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.my.commonlibrary.R;
import com.my.commonlibrary.adapter.LeftMenuAdapter;
import com.my.commonlibrary.database.dao.Person;
import com.my.commonlibrary.database.db.TableConfig;
import com.my.commonlibrary.database.db.TableOperate;
import com.my.commonlibrary.fragment.HomeTabViewPagerFragment;
import com.my.commonlibrary.litedb.model.Student;
import com.my.commonlibrary.model.LeftMenuEntity;
import com.my.commonlibrary.utils.ScreenUtil;
import com.my.commonlibrary.utils.ToastUtil;

import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Author : Cooper
 * Time : 2016/9/7  15:07
 * Description :  侧滑菜单  Drawerlayout
 */
public class HomeDrawerLayoutActivity extends FragmentActivity implements AdapterView.OnItemClickListener{

    @Bind(R.id.content_fl)
    FrameLayout contentFl;
    @Bind(R.id.left_menu_lv)
    ListView leftMenuLv;
    @Bind(R.id.drawerLayout)
    DrawerLayout drawerLayout;

    private LeftMenuAdapter mLeftMenuAdapter;
    private List<LeftMenuEntity> leftMenuEntityList = new ArrayList<>();

    private FragmentManager mFragmentManager;

    private String[] menuNames = {"我的首页","精彩发现","新闻中心","在线听歌免流量",
            "听歌识曲","主题换肤","夜间模式"};
    private int[] icons = {R.drawable.topmenu_icn_msg,R.drawable.topmenu_icn_store,
            R.drawable.topmenu_icn_vip,R.drawable.topmenu_icn_free,R.drawable.topmenu_icn_identify,
            R.drawable.topmenu_icn_night,R.drawable.topmenu_icn_skin};

    private WindowManager windowManager;
    private WindowManager.LayoutParams wlp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_drawer_layout);
        ButterKnife.bind(this);
        mFragmentManager = getSupportFragmentManager();
        initLeftMenuData();
        initLeftMenuView();

        switchFragment("我的首页");

//        initOriginDB();

//        initLitePalDB();

//        initWindowManager();
//        showNewGuideImage();

//        addGuideImage1();
    }
    /**
     * LitePal数据库封装运用
     */
    private void initLitePalDB() {
        SQLiteDatabase db = Connector.getDatabase();
        Student student = new Student();
        student.setName("Alex");
        student.setAge("18");
        student.setSex("男");
        student.save();
    }

    /**
     * 原始数据库封装运用
     */
    private void initOriginDB() {
        TableOperate tableOperate = new TableOperate();
        ArrayList<Person> arrayList = new ArrayList<>();
        arrayList = tableOperate.query(TableConfig.TABLE_PERSON,Person.class,TableConfig.Person.NAME,"Jim");
        Log.e("DbInit",arrayList.get(0).getAddress());
    }

    /**
     * 初始化左侧menu数据
     */
    private void initLeftMenuData() {
        for (int i=0;i<5;i++) {
            LeftMenuEntity leftMenuEntity = new LeftMenuEntity();
            leftMenuEntity.icon_rs = icons[i];
            leftMenuEntity.menuName = menuNames[i];
            if (i == 3) {
                leftMenuEntity.isShowLine = true;
            }
            leftMenuEntityList.add(leftMenuEntity);
        }
    }

    /**
     * 初始化左侧menu的view
     */
    private void initLeftMenuView() {
        mLeftMenuAdapter = new LeftMenuAdapter(this, leftMenuEntityList);
        leftMenuLv.setAdapter(mLeftMenuAdapter);
        leftMenuLv.setOnItemClickListener(this);

        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.e("ah"," setDrawerListener  onDrawerSlide");
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.e("ah"," setDrawerListener  onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.e("ah"," setDrawerListener  onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                switch (newState) {
                    case DrawerLayout.STATE_DRAGGING: // 手动拖拽状态
                        Log.e("newState"," setDrawerListener  onDrawerStateChanged === STATE_DRAGGING");
                        break;
                    case DrawerLayout.STATE_IDLE: // view停止状态
                        Log.e("newState"," setDrawerListener  onDrawerStateChanged === STATE_IDLE");
                        break;
                    case DrawerLayout.STATE_SETTLING: // view从手指离开到view停止的那段时间状态
                        Log.e("newState"," setDrawerListener  onDrawerStateChanged === STATE_SETTLING");
                        break;
                }
                Log.e("ah"," setDrawerListener  onDrawerStateChanged");
            }
        });
    }

    private void switchFragment(String menuItem) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        switch (menuItem) {
            case "我的首页":
                transaction.replace(R.id.content_fl,new HomeTabViewPagerFragment());
                transaction.commit();
                break;
            case "精彩发现":
                break;
            case "新闻中心":
                break;
            case "在线听歌免流量":
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        drawerLayout.closeDrawers();
        LeftMenuEntity leftMenuEntity = leftMenuEntityList.get(i);
        ToastUtil.showToast(leftMenuEntity.menuName);
        switchFragment(leftMenuEntity.menuName);
    }

    private void initWindowManager() {
        windowManager = getWindowManager();
        wlp = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        wlp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
//        wlp.format = PixelFormat.RGBA_8888;
        wlp.gravity = Gravity.TOP | Gravity.LEFT;
        wlp.width = ScreenUtil.getScreenWidth(this);
        wlp.height = ScreenUtil.getScreenHeight(this);
    }

    private void showNewGuideImage() {
        final ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.new_guide_bg));
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        windowManager.addView(imageView,wlp);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                windowManager.removeView(imageView);
            }
        });
    }


    private int[] imgList = {R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3,R.drawable.guide_4,R.drawable.guide_5};

    /**
     * 显示新手引导图
     */
    public void addGuideImage1() {
        View view = this.getWindow().getDecorView().findViewById(R.id.drawerLayout);//查找通过setContentView上的根布局
        if(view==null)return;
        ViewParent viewParent = view.getParent();
        if(viewParent instanceof FrameLayout){
            final FrameLayout frameLayout = (FrameLayout)viewParent;
            createImage(frameLayout);
        }
    }

    private int num = 0;
    private void createImage(final FrameLayout viewParent) {
        if (num == imgList.length) {
            return;
        }
        final ImageView guideImage = new ImageView(this);
        guideImage.setAlpha(40);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        guideImage.setLayoutParams(params);
        guideImage.setScaleType(ImageView.ScaleType.FIT_XY);
        guideImage.setImageResource(imgList[num]);
        guideImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewParent.removeView(guideImage);
                createImage(viewParent);
            }
        });
        viewParent.addView(guideImage);
        num++;
    }
}
